Calling function stored in Skip List?

Seems I can save functions in a skip list (see below).

However I am not sure how I can call a function saved to a skip list...is this possible? In other words, how do I loop through that skip list of functions to get something I can call?

 

Module m=current Module

//Define 3 functions:
void f1(Module m, string s) { print "1. Module '" name(m) "': string is '" s "'\n" }
void f2(Module m, string s) { print "2. Module '" name(m) "': string is '" s "'\n" }
void f3(Module m, string s) { print "3. Module '" name(m) "': string is '" s "'\n" }

//Function to call another function: 
void fCaller(Module m,string s,void f(Module,string)) {f(m,s)}
 
//Call the 3 defined functions using fCaller():
fCaller(m,"No.1",f1)
fCaller(m,"No.2",f2)
fCaller(m,"No.3",f3)

//Now another approach...save the three functions to a skip list:
Skip ruleFuncs=create
int key=0
put(ruleFuncs,key++,f1)
put(ruleFuncs,key++,f2)
put(ruleFuncs,key++,f3)

//Now HOW do you retrieve a function from the skip list and call it?
void func
for func in ruleFuncs do
{
        //fCaller(m,func) //this doesn't work
}
delete ruleFuncs

 


strathglass - Sun Apr 08 16:17:12 EDT 2018

Re: Calling function stored in Skip List?
O.Wilkop - Mon Apr 09 04:21:09 EDT 2018

int func;
for func in ruleFuncs do
{
        fCaller(m, "No.x", addr_ func)
}

 

try this instead

Re: Calling function stored in Skip List?
strathglass - Mon Apr 09 06:57:47 EDT 2018

O.Wilkop - Mon Apr 09 04:21:09 EDT 2018

int func;
for func in ruleFuncs do
{
        fCaller(m, "No.x", addr_ func)
}

 

try this instead

Thanks...doesn't help that I forgot the string, but the "addr_" function is new to me...

When I tried to add the addr_ call, it stops the syntax errors but it doesn't actually do anything: in the update below the first two functions called explicityly via fCaller() work.

But the two calls from inside the for loop do not execute (nothing printed).

Any other ideas?

//Define 2 functions:
void f1(Module m, string s) { print "1. Module '" name(m) "': string is '" s "'\n" }
void f2(Module m, string s) { print "2. Module '" name(m) "': string is '" s "'\n" }
//Function to call another function: 
void fCaller(Module m,string s,void f(Module,string)) {f(m,s)}
Module m=current
//Call the 2 defined functions using fCaller():
fCaller(m,"No.1",f1)
fCaller(m,"No.2",f2)
//Now another approach...save the two functions to a skip list:
Skip ruleFuncs=create
int key=0
put(ruleFuncs,key++,f1)
put(ruleFuncs,key++,f2)
//Call functions saved to skip list:
void func
int i=0
for func in ruleFuncs do
{
        i++
        fCaller(m,"#" i"",addr_ func)
}
delete ruleFuncs

 

Re: Calling function stored in Skip List?
O.Wilkop - Mon Apr 09 07:04:40 EDT 2018

strathglass - Mon Apr 09 06:57:47 EDT 2018

Thanks...doesn't help that I forgot the string, but the "addr_" function is new to me...

When I tried to add the addr_ call, it stops the syntax errors but it doesn't actually do anything: in the update below the first two functions called explicityly via fCaller() work.

But the two calls from inside the for loop do not execute (nothing printed).

Any other ideas?

//Define 2 functions:
void f1(Module m, string s) { print "1. Module '" name(m) "': string is '" s "'\n" }
void f2(Module m, string s) { print "2. Module '" name(m) "': string is '" s "'\n" }
//Function to call another function: 
void fCaller(Module m,string s,void f(Module,string)) {f(m,s)}
Module m=current
//Call the 2 defined functions using fCaller():
fCaller(m,"No.1",f1)
fCaller(m,"No.2",f2)
//Now another approach...save the two functions to a skip list:
Skip ruleFuncs=create
int key=0
put(ruleFuncs,key++,f1)
put(ruleFuncs,key++,f2)
//Call functions saved to skip list:
void func
int i=0
for func in ruleFuncs do
{
        i++
        fCaller(m,"#" i"",addr_ func)
}
delete ruleFuncs

 

"func" needs to be of type "int", not "void".

 

Using int to loop through skiplists basically gives you a pointer to where the values were stored, which you can then dereference with the addr_ call to get the actual value. This is useful if you store different types in the same skiplist or to call functions from a skiplist (like in your example with a wrapper)

Re: Calling function stored in Skip List?
strathglass - Mon Apr 09 07:07:37 EDT 2018

Excellent, thanks ... that worked!! I will make a note of this trick. :)